package com.yzt.message.msgdialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.yzt.zhuju.dialog.TopBaseDialog;
import com.yzt.message.R;

import io.rong.imkit.conversationlist.model.BaseUiConversation;


/**
 * desc: 消息悬浮弹出框
 **/
@Deprecated
public class MsgFloatFragmentDialog extends TopBaseDialog {

    private Handler mHandler;
    private MsgFloatView mMsgFloat;
    private BaseUiConversation mUiConversation;

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null && getDialog().getWindow() != null) {
            // 设置宽度为屏宽, 靠近屏幕底部。
            Window win = getDialog().getWindow();
            // 一定要设置Background，如果不设置，window属性设置无效
            win.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = win.getAttributes();
            params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            win.setAttributes(params);
        }
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.TopMsgDialogCustomeStyle);
        mHandler = new Handler();
        mHandler.postDelayed(MsgFloatFragmentDialog.this::dismiss, MsgFloatDialogHelper.SHOW_DIALOG_TIME);
    }

    public CharSequence getData() {
        Bundle bundle = getArguments();
        mUiConversation = bundle.getParcelable("data");
        CharSequence content;
        if (mUiConversation.mCore.getMentionedCount() != 0) {
            content = MsgFloatDialogHelper.handleMentionedContent(getContext(), mUiConversation, this.getMsgFloat().getTvContent());
        } else {
            content = MsgFloatDialogHelper.handleCommonContent(getContext(), mUiConversation, this.getMsgFloat().getTvContent());
        }
        return content;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.dialog_msg_float;
    }

    @Override
    public void bindView(View v) {
        CharSequence showContentBundle = getData();
        mMsgFloat = v.findViewById(R.id.mfv_msg_float);
        mMsgFloat.set(mUiConversation.mCore.getPortraitUrl(), mUiConversation.mCore.getConversationTitle(), showContentBundle, mUiConversation.mCore.getTargetId());
        mMsgFloat.setOnClickListener(v1 -> dismiss());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
        mHandler = null;
    }

    public MsgFloatView getMsgFloat() {
        return mMsgFloat;
    }
}
